<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">称号规则</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>称号规则</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <form class="layui-form toolbar" name="queryForm">
            描述：
            <input name="ttrule" class="layui-input search-input enterSubmit" type="text"
                   placeholder="输入规则描述关键字"/>&emsp;
            所属称号类型：
            <select name="typeId" id="typeId" lay-verify="required">
                <option value="-1">全部</option>
            </select>
            <button id="titleRule-btn-search" class="layui-btn icon-btn" lay-submit lay-filter="query"><i
                    class="layui-icon">&#xe615;</i>搜索
            </button>

            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button id="titleRule-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </form>

        <!-- 数据表格 -->
        <table class="layui-table" id="titleRule-table" lay-filter="titleRule-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="titleRule-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;


        let url = config.base_server + 'api-user/titleRule/getSelectTitleRules';
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'tttypeName',  title: '所属称号类型', align: 'center'},
            {field: 'ttcode', sort: true, title: '规则编号', align: 'center'},
            {field: 'ttrule', sort: true, title: '规则描述', align: 'center'},
            {field: 'points', sort: true, title: '积分值', align: 'center'},
            {
                field: 'cretim', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.cretim);
                }, title: '创建时间'
            },
            {
                field: 'updtim', sort: true, align: 'center', templet: function (d) {
                    return util.toDateString(d.updtim);
                }, title: '修改时间'
            },
            {templet: '#titleRule-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#titleRule-table',
            url: url,
            method: 'post',
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(titleRule-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-user/titleRule/delTitleRules' + obj.data.ttruid, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('titleRule-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            }
        });

        // 添加按钮点击事件
        $('#titleRule-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            admin.putTempData('titleRule', data);
            let title = data ? '修改积分规则' : '添加积分规则';
            admin.popupCenter({
                title: title,
                path: 'pages/system/titleRule_form.html',
                area: '800px',
                offset: '200px',
                finish: function () {
                    table.reload('titleRule-table', {});
                }
            });
        };

        // 条件检索
        form.on('submit(query)', (data) => {
            table.reload('titleRule-table', {where: data.field});
            return false;
        });


        // 回车查询
        $('.enterSubmit').keyup((e) => {
            switch (e.keyCode) {
                case 13:
                    $('#titleRule-btn-search').click();
                    break;
            }
        });


        // 获取所有称号类型
        layer.load(2);
        admin.req('api-user/titleType/getSelectTitleTypes', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                $.each(data.data,function(index,item){
                    $('#typeId').append(new Option(item.tpname,item.tttpid));//往下拉菜单里添加元素
                });
                form.render();  //菜单渲染 把内容加载进去
            } else {
                layer.msg('获取称号类型失败', {icon: 2, time: 500});
            }
        }, 'post');

    });
</script>